/*
 * $Id: Desktop.java 88 2009-04-24 07:40:25Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.gui.deltagui.widget;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

import deltadesk.gui.deltagui.graphics.Renderable;


/**
 * Desktop.
 * 
 * @author Andreas Meyer
 * @version $Revision: 88 $
 */
public class Desktop implements Renderable {
	
	/** The default desktop background color. */
	private static final Color PANEL_BACKGROUND = new Color(101, 113, 133);
	
	/** Show frames per second. */
	private boolean showFPS;
	
	/** The current time. */
	private long currentTime;
	
	/** The frame counter. */
	private int frameCounter = 0;
	
	/** The frames per second. */
	private int fps;
	
	/** The width. */
	private int width;
	
	/** The height. */
	private int height;

	/** The main composite. */
	protected Composite mainComposite;


	/**
	 * Construct the desktop.
	 * 
	 */
	public Desktop() {
		setMainComposite(new DeltaPanel());
	}

	/**
	 * @param mainComposite the mainComposite to set
	 */
	public void setMainComposite(final Composite mainComposite) {

		if(mainComposite != null) {
			this.mainComposite = mainComposite;	
		}

		this.mainComposite.setBackgroundColor(PANEL_BACKGROUND);
		this.mainComposite.setParent(null);
		this.mainComposite.setSize(width, height);	
		this.mainComposite.setFocus();

		currentTime = System.currentTimeMillis();
	}

	/**
	 * Set the desktop size.
	 * Delegate to main control.
	 * 
	 * @param width
	 * @param height
	 */
	public void setSize(final int width, final int height) {
		this.width  = width;
		this.height = height;
		this.mainComposite.setSize(width, height);	
	}

	/**
	 * Remove all child controls from this composite.
	 * 
	 */
	public void removeAll() {
		mainComposite.removeAll();
	}

	/**
	 * @param showFPS the showFPS to set
	 */
	public void setShowFPS(final boolean showFPS) {
		this.showFPS = showFPS;
	}

	/**
	 * Render the desktop.
	 * Validate and render the mainControl.
	 * 
	 * @param g2
	 */
	public void render(final Graphics2D g2) {

		mainComposite.render(g2);

		if(showFPS) {
			
			frameCounter++;
			if((System.currentTimeMillis() - currentTime) > 1000) {
				
				currentTime = System.currentTimeMillis();
				fps = frameCounter;
				frameCounter = 0;
			} // end if

			if(fps > 0) {

				g2.setColor(mainComposite.getBackgroundColor());
				g2.fillRect(0, 0, 60, 15);
				g2.setColor(Color.BLACK);
				g2.drawString(fps+ " fps", 2, 12);
			} // end if
		} // end if
	}
	
	/**
	 * Post a key event.
	 * Delegate to main control.
	 * 
	 * @param ke
	 */
	public void postKeyEvent(final KeyEvent ke) {
		mainComposite.postKeyEvent(ke);
	}

}
